
#include <QFile>

#include "RendezVous.h"

QList<RendezVous> RendezVous::GetRendezVous()
{
	QFile file("xml/meetings.xml");
	QList<RendezVous> rdvs;

	if(file.open(QIODevice::ReadOnly | QIODevice::Text))
	{
		QXmlStreamReader reader(&file);
		while(!reader.atEnd())
		{
			QXmlStreamReader::TokenType token = reader.readNext();
			if(token == QXmlStreamReader::StartDocument)
				continue; //  cxxx)========>  (épée)

			if(token == QXmlStreamReader::EndElement)
				break; //  cxxx)========>  (épée)

			if(token == QXmlStreamReader::StartElement)
				rdvs.append(ParseRendezVous(reader));
		}

		file.close();
		reader.clear();
	}

	return rdvs;
}

RendezVous RendezVous::ParseRendezVous(QXmlStreamReader& xml)
{
	RendezVous rdv;
	Compatibilite c;

	xml.readNext();
	
	while(!(xml.tokenType() == QXmlStreamReader::EndElement && xml.name() == "rdv"))
	{
		QXmlStreamReader::TokenType token = xml.readNext();

		if(token == QXmlStreamReader::EndElement && xml.name() == "rdvs")
			break;

		if(token == QXmlStreamReader::EndElement)
			continue;

		if(xml.name() == "tauxCompatibilite")
		{
			xml.readNext();
			c.SetTauxCompatibilite(xml.text().toString().toInt());
		}

		if(xml.name() == "client1")
		{
			c.SetClient1(TrueLoveClient::ParseClient(xml));
		}

		if(xml.name() == "client2")
		{
			c.SetClient2(TrueLoveClient::ParseClient(xml));
		}

		if(xml.name() == "apparenceTo1")
		{
			xml.readNext();
			rdv.SetApparenceTo1(xml.text().toString().toInt());
		}

		if(xml.name() == "charmeTo1")
		{
			xml.readNext();
			rdv.SetCharmeTo1(xml.text().toString().toInt());
		}

		if(xml.name() == "humourTo1")
		{
			xml.readNext();
			rdv.SetHumourTo1(xml.text().toString().toInt());
		}

		if(xml.name() == "intelligenceTo1")
		{
			xml.readNext();
			rdv.SetIntelligenceTo1(xml.text().toString().toInt());
		}
		
		if(xml.name() == "conversationTo1")
		{
			xml.readNext();
			rdv.SetConversationTo1(xml.text().toString().toInt());
		}

		if(xml.name() == "gentillesseTo1")
		{
			xml.readNext();
			rdv.SetGentillesseTo1(xml.text().toString().toInt());
		}
		
		if(xml.name() == "apparenceTo2")
		{
			xml.readNext();
			rdv.SetApparenceTo2(xml.text().toString().toInt());
		}

		if(xml.name() == "charmeTo2")
		{
			xml.readNext();
			rdv.SetCharmeTo2(xml.text().toString().toInt());
		}

		if(xml.name() == "humourTo2")
		{
			xml.readNext();
			rdv.SetHumourTo2(xml.text().toString().toInt());
		}

		if(xml.name() == "intelligenceTo2")
		{
			xml.readNext();
			rdv.SetIntelligenceTo2(xml.text().toString().toInt());
		}
		
		if(xml.name() == "conversationTo2")
		{
			xml.readNext();
			rdv.SetConversationTo2(xml.text().toString().toInt());
		}

		if(xml.name() == "gentillesseTo2")
		{
			xml.readNext();
			rdv.SetGentillesseTo2(xml.text().toString().toInt());
		}
	}

	rdv.SetCompatibilite(c);

	return rdv;
}
